{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.输入某年某月某日，判断这一天是这一年的第几天？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "359\n"
     ]
    }
   ],
   "source": [
    "def tissue1(year, month, day):\n",
    "    days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "    if (year % 4 == 0 and year % 100!= 0) or (year % 400 == 0):   #判断是否为闰年\n",
    "        days_in_month[1] = 29\n",
    "    day_count = 0\n",
    "    for i in range(month - 1):\n",
    "        day_count += days_in_month[i]\n",
    "    day_count += day\n",
    "    return day_count\n",
    "#input(\"请输入日期(以逗号隔开)\")\n",
    "print(tissue1(2024,12,24))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.判断1000中有多少个素数，并输出所有素数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "prime=[]\n",
    "for num in range(2,1001):                           \n",
    "    is_prime=True\n",
    "    for i in range(2,int(num**0.5)+1):  #遍历可能的因数     \n",
    "        if num % i ==0:\n",
    "            is_prime=False\n",
    "            break\n",
    "    if is_prime:\n",
    "        prime.append(num)\n",
    "print(prime)        \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。（要求有键盘输入）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成绩为： 90\n",
      "A\n"
     ]
    }
   ],
   "source": [
    "def charge_score(score):\n",
    "    if score>=90:\n",
    "        print(\"A\")\n",
    "    elif score >=60:\n",
    "        print(\"B\") \n",
    "    else:\n",
    "        print(\"C\")\n",
    "\n",
    "score=int(input('请输入成绩'))\n",
    "print('成绩为：',score)\n",
    "charge_score(score)\n",
    "        \n",
    "\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数字，字母，其他字符的个数分别是：\n",
      "(7, 7, 4)\n"
     ]
    }
   ],
   "source": [
    "def count_str(str1):\n",
    "    count_str_num=0\n",
    "    count_str_word=0\n",
    "    count_str_other=0\n",
    "    for i in str1:\n",
    "        if i in \"0123456789\":             #利用字符串判断是否为数字\n",
    "            count_str_num+=1\n",
    "        elif 65<=ord(i)<=90 or 97<=ord(i)<=122:            #利用字母的ASCII码判断是否为字母\n",
    "            count_str_word+=1\n",
    "        else:\n",
    "            count_str_other+=1\n",
    "    return(count_str_num,count_str_word,count_str_other)       \n",
    "print(\"数字，字母，其他字符的个数分别是：\") \n",
    "print(count_str(\"1231242sahduwe][;]\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9.一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6, 28, 496]\n"
     ]
    }
   ],
   "source": [
    "num=[]\n",
    "for i in range(1,1001):\n",
    "    yinshu_sum=0\n",
    "    for j in range(1,i):\n",
    "        if i % j == 0:\n",
    "            yinshu_sum+=j  #分解因子\n",
    "    if yinshu_sum==i:\n",
    "        num.append(i)\n",
    "print(num)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "11.入任意数N，求1+2!+3!+...+N!的值（注意实数范围）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "153\n"
     ]
    }
   ],
   "source": [
    "def jiecheng(n):\n",
    "    if n==1 or n==0 :\n",
    "       return 1\n",
    "    else:\n",
    "        return(jiecheng(n-1)*n) #递归方式求n的阶乘\n",
    "    \n",
    "def jiecheng_sum(n):\n",
    "    sum=0\n",
    "    for i in range(1,n+1):\n",
    "        sum+=jiecheng(i)        #循环求1到n的阶乘的和\n",
    "    return(sum)\n",
    "\n",
    "print(jiecheng_sum(5))    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "13.输入任意奇数N，按如下要求打印出图案（菱形）\n",
    "   *\n",
    "  ***\n",
    " *****\n",
    "*******（N控制行数和本行的星数）\n",
    " *****\n",
    "  ***\n",
    "   *\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  *\n",
      " ***\n",
      "*****\n",
      " ***\n",
      "  *\n"
     ]
    }
   ],
   "source": [
    "def lingxing(n):\n",
    "    for i in range(1,2*n+1,2):\n",
    "        print(\" \"*(n-(i+1)//2)+\"*\"*i)\n",
    "    \n",
    "    for j in range(1,n):\n",
    "        print(\" \"*(j)+\"*\"*(2*(n-j)-1))\n",
    "   \n",
    "\n",
    "       \n",
    "\n",
    "n=int(input('请输入一个奇数'))\n",
    "lingxing(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "15.对10个数进行排序。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "list1 [45, 23, 78, 12, 34, 67, 89, 10, 56, 91]\n",
      "[10, 12, 23, 34, 45, 56, 67, 78, 89, 91]\n",
      "list2 [12, 34, 56, 78, 81, 90, 22, 44, 66, 88]\n",
      "[12, 22, 34, 44, 56, 66, 78, 81, 88, 90]\n",
      "list3 [77, 55, 33, 11, 99, 88, 66, 44, 22, 10]\n",
      "[10, 11, 22, 33, 44, 55, 66, 77, 88, 99]\n"
     ]
    }
   ],
   "source": [
    "#首先python中自带了sort函数可以对列表进行排序\n",
    "import random\n",
    "#冒泡排序\n",
    "def bubble_sort(arr):\n",
    "    n = len(arr)\n",
    "\n",
    "    for i in range(n):\n",
    "        for j in range(0, n - i - 1):\n",
    "            if arr[j] > arr[j + 1]:\n",
    "                arr[j], arr[j + 1] = arr[j + 1], arr[j]\n",
    "\n",
    "    return arr\n",
    "\n",
    "#插入排序\n",
    "def insertion_sort(arr):\n",
    "    for i in range(1, len(arr)):\n",
    "        current_value = arr[i]\n",
    "        position = i\n",
    "        while position > 0 and arr[position - 1] > current_value:\n",
    "            arr[position] = arr[position - 1]\n",
    "            position -= 1\n",
    "        arr[position] = current_value\n",
    "    return arr\n",
    "\n",
    "list_1=[45, 23, 78, 12, 34, 67, 89, 10, 56, 91]\n",
    "list_2=[12, 34, 56, 78, 81, 90, 22, 44, 66, 88]\n",
    "list_3=[77, 55, 33, 11, 99, 88, 66, 44, 22, 10]\n",
    "#for i in range(0,10):\n",
    "    #list_1.append(random.randint(1,100))\n",
    "    #list_2.append(random.randint(1,100))\n",
    "    #list_3.append(random.randint(1,100))\n",
    "print(\"list1:\",list_1)\n",
    "print(bubble_sort(list_1))\n",
    "print(\"list2:\",list_2)\n",
    "print(insertion_sort(list_2))\n",
    "print(\"list3:\",list_3)\n",
    "list_3.sort()\n",
    "print(list_3)    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "17.有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 14, 15, 16, 50]\n"
     ]
    }
   ],
   "source": [
    "#生成升序列表\n",
    "import random\n",
    "\n",
    "list_4=[1,2,3,4,5,6,7,8,9,10,11,12,12,14,15,16]\n",
    "\n",
    "\n",
    "def insert_list(num):\n",
    "    for i in range(0,15):\n",
    "        if list_4[i]>=num:\n",
    "            list_new=list_4.insert(i,num)\n",
    "            break\n",
    "    else:\n",
    "            list_4.append(num)\n",
    "\n",
    "\n",
    "\n",
    "insert_num=int(input(\"请输入一个随机数\"))\n",
    "insert_list(insert_num)\n",
    "print(list_4)\n",
    "# list_4.append(insert_1)\n",
    "# list_new=list_4.sort() \n",
    "# 人生苦短我用Python \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "19.有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7]\n"
     ]
    }
   ],
   "source": [
    "#约瑟夫环问题\n",
    "\n",
    "def jose(n):\n",
    "      list1=[]\n",
    "      for i in range(1,n+1):\n",
    "           list1.append(i) #创建n人的列表队列\n",
    "           count=0\n",
    "      while len(list1)>1:\n",
    "          count+=1\n",
    "          tmp=list1.pop(0)     #将队列头元素出队\n",
    "          if count!=3:\n",
    "               list1.append(tmp) #不退出的元素重新回到队尾\n",
    "          else:\n",
    "               count=0\n",
    "      return list1         \n",
    "\n",
    "temp=int(input('输入一个号码'))          \n",
    "print(jose(temp)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。（要求有键盘输入）\n",
      "\n",
      "成绩为： 2\n",
      "C\n"
     ]
    }
   ],
   "source": [
    "import nbformat\n",
    "from IPython.display import display\n",
    "from IPython.core.getipython import get_ipython\n",
    "import io\n",
    "import types\n",
    "import random\n",
    " \n",
    "random.seed(0) \n",
    "\n",
    "# 获取当前Notebook文件名，不同环境获取方式略有不同，这里简单示例一种\n",
    "notebook_name = \"期末作业.ipynb\"\n",
    "\n",
    "# 读取当前Notebook文件内容\n",
    "with open(notebook_name, 'r', encoding='utf-8') as f:\n",
    "    notebook_content = nbformat.read(f, as_version=4)\n",
    "\n",
    "#输入需要查找的cell\n",
    "\n",
    "t=int(input('输入查找题目的索引'))        #注意python索引从0开始，即一号问题索引为0\n",
    "\n",
    "\n",
    "# 指定要输出的cell的索引范围，这里示例选择第2到第4个cell（索引1到3），可按需修改\n",
    "start_index = 2*t\n",
    "end_index = 2*t+1\n",
    "\n",
    "# 创建一个临时的全局命名空间，用于执行代码cell时存放变量等\n",
    "global_namespace = {}\n",
    "for index, cell in enumerate(notebook_content.cells):\n",
    "    if start_index <= index <= end_index:\n",
    "        if cell.cell_type == \"markdown\":\n",
    "            print(cell.source)\n",
    "        elif cell.cell_type == \"code\":\n",
    "            try:\n",
    "                compiled_code = compile(cell.source, '<string>', 'exec')\n",
    "                exec(compiled_code, global_namespace)\n",
    "                # 检查是否有返回结果（例如有表达式求值等情况）\n",
    "                if '__builtin__' in global_namespace:\n",
    "                    result = global_namespace['__builtin__']\n",
    "                    if isinstance(result, (int, float, str, list, tuple, dict, set)):\n",
    "                        display(result)\n",
    "            except Exception as e:\n",
    "                print(f\"执行代码cell {index} 出现错误: {e}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
